//    Copyright (C) Kherty.  All rights reserved.
using System;
using System.Windows;
using System.Windows.Media;
using OpenLS.Core;
using OpenLS.Core.Serialization;
using OpenLS.Spreadsheet.Internals;
using OpenLS.Spreadsheet.Office2003;

namespace OpenLS.Spreadsheet
{
    partial class FormattingRecord: IOffice2003XmlSerializable
    {
        #region IXmlSpreadsheetSerializable Members

#if DEBUG
        public string XmlId;
#endif
        void IOffice2003XmlSerializable.ReadXmlSpreadsheet(ReadContext context)
        {
            StyleLookup l = context.GetData<StyleLookup>();
            using (ReadContext c = context.Read("Style"))
            {
                l.Add(c.GetString(XmlSpreadsheetConstants.XmlSpreadsheetNamespace + "ID"), (CellFormattingRecord) this);
#if DEBUG
                this.XmlId = c.GetString(XmlSpreadsheetConstants.XmlSpreadsheetNamespace + "ID");
#endif
                if (c.ReaderLocalName == "Alignment")
                {
                    using (ReadContext cAlignment = context.Read("Alignment"))
                    {
                        //\\ this.HorizontalAlignment = cAlignment.GetOptionalEnum<HorizontalCellAlignment>("Horizontal", cAlignment.NamespaceUri);
                        VerticalCellAlignment2003? v = cAlignment.GetOptionalEnum<VerticalCellAlignment2003>(cAlignment.Namespace + "Vertical");
                        if (v != null && v.Value != VerticalCellAlignment2003.Automatic)
                            _verticalAlignment = (VerticalCellAlignment)v.Value;
                        {
                            HorizontalAlignment2003 a = cAlignment.GetOptionalEnum(cAlignment.Namespace + "Horizontal", HorizontalAlignment2003.Automatic);
                            if (a != HorizontalAlignment2003.Automatic)
                                _horizontalAlignment = (HorizontalCellAlignment)a;
                        }
                        _indentation = cAlignment.GetOptionalDouble(cAlignment.Namespace + "Indent");
                        if (cAlignment.GetOptionalBoolean(cAlignment.Namespace + "WrapText", false))
                            _textWrapping = TextWrapping.Wrap;
                        if (cAlignment.GetOptionalBoolean(cAlignment.Namespace + "VerticalText", false))
                        {
                            //\\todo
                        }
                        _textRotation = c.GetOptionalInteger(cAlignment.Namespace + "Rotate", 0);
                    }
                }
                if (c.ReaderLocalName == "Borders")
                {
                    Borders = new Borders();
                    IOffice2003XmlSerializable s = Borders;
                    s.ReadXmlSpreadsheet(c);
                }
                if (c.ReaderLocalName == "Font")
                {
                    Font = new Font();
                    ((IOffice2003XmlSerializable)Font).ReadXmlSpreadsheet(c);
                }
                if (c.ReaderLocalName == "Interior")
                {
                    ReadInterior(c);
                }
                if (c.ReaderLocalName == "NumberFormat")
                {
                    using (c.Read("NumberFormat"))
                    {
                        string f = c.GetOptionalString(c.Namespace + "Format");
                        switch (f)
                        {
                            case null: break;
                            case "General":
                            case "General Number":
                            case "General Date":
                            case "Long Date":
                            case "Medium Date":
                            case "Short Date":
                            case "Long Time":
                            case "Medium Time":
                            case "Short Time":
                            case "Currency":
                            case "Euro Currency":
                            case "Fixed":
                            case "Standard":
                            case "Percent":
                            case "Scientific":
                            case "Yes/No":
                            case "True/False":
                            case "On/Off":
                                Format = Core.Formats.General;
                                break;
                            default:
                                Format = Format.Parse(f); break;
                        }
                    }
                }
                c.EatForNow("Protection");
            }
        }

        private void ReadInterior(ReadContext context)
        {
            using (ReadContext c = context.Read("Interior"))
            {
                string color = c.GetOptionalString(c.Namespace + "Color");
                if (color != null)
                {
                    PatternFill f = new PatternFill((Color) ColorConverter.ConvertFromString(color), null,
                                                    PatternType.Solid);
                    Background = f;
                }
                //\\ todo: patterns
            }
        }

        #endregion
    }
}
